package com.planegame;

import com.planegame.Obj.*;
import com.planegame.utils.Gameutils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static javax.swing.UIManager.get;

public class GameWin extends JFrame {

    //游戏状态，0未开始 1游戏中 2暂停 3失败 4成功
    public static int state=0;
    //游戏重绘次数
    int count=1;
    //缓存图片
    Image offScreenImage = null;
    //游戏物体
    public BgObj bgObj = new BgObj(Gameutils.bgImg,0,-2000,2);

    public PlaneObj planeObj = new PlaneObj(Gameutils.planeImg,290,550,20,30,0,this);

    public ShellObj shellObj=new ShellObj(Gameutils.shellImg,planeObj.getX()+3,planeObj.getY()-16,14,29,5,this);

    public BossObj bossObj=new BossObj(Gameutils.bossImg,250,10,85,83,2,this);

    public void launch() {
        this.setVisible(true);//设置窗口是否可见
        this.setSize(600, 600);//设置窗口大小
        this.setLocationRelativeTo(null);//设置窗口位置到中间
        this.setTitle("飞机大战");//设置窗口标题


        //Gameutils->工具类
        //将背景和自身飞机加入gameObjList

        Gameutils.gameObjList.add(bgObj);
        Gameutils.gameObjList.add(planeObj);


        //匿名内部类
        //程序边paint()边听鼠标
        this.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==1&&state==0){
                    state=1;repaint();
                }
                if(e.getButton()==1&&state==3){
                    state=0;
                }
            }
        });

        while(true){
            if(state==0)repaint();
            if(state==1){
                createObj();//构造的方法,制造子弹和敌机到gameObjList
                repaint();
            }
            //设置帧率
            try{
                Thread.sleep(20);//休眠20毫秒
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    public void paint(Graphics g) {
        //双缓存
        if (offScreenImage == null) {
            offScreenImage = createImage(600, 600);
        }

        //Graphics是个类，相当于画笔
        //这使得每一次paint都绘制的是上一次的截图，解决了文字闪动问题//不是很看得懂
        //注意graphics和image的区别

        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0, 0, 600, 600);

        if (state == 0) {
            //gImage是刚开始创建的空的图像
            gImage.drawImage(Gameutils.bgImg, 0, 0, null);
            gImage.drawImage(Gameutils.bossImg, 250, 10, null);
            gImage.drawImage(Gameutils.explodeImg, 270, 350, null);
            //设置文字颜色，字体，大小
            gImage.setColor(Color.yellow);
            gImage.setFont(new Font("仿宋", Font.BOLD, 40));
            gImage.drawString("点击开始游戏", 180, 300);
        }

        if (state == 1) {
            //绘制每一个游戏元素
            for (int i = 0; i < Gameutils.gameObjList.size(); i++) {
                Gameutils.gameObjList.get(i).paintSelf(gImage);
            }
            //将gameObjlist中的需要remove的remove了
            Gameutils.gameObjList.removeAll(Gameutils.removeList);
        }
        if(state==2){
            gImage.setColor(Color.yellow);
            gImage.setFont(new Font("仿宋", Font.BOLD, 80));
            gImage.drawString("游戏胜利", 180, 300);
        }
        if(state==3){
            //在上一张offScreenImage
            gImage.setColor(Color.red);
            gImage.setFont(new Font("仿宋", Font.BOLD, 40));
            gImage.drawString("Game Over", 180, 300);
            gImage.drawString("点击复活",180,400);
        }
        //绘制offScreenImage
        g.drawImage(offScreenImage, 0, 0, null);

        count++;
    }

    void createObj(){
        if(count%10==0) {
            Gameutils.shellObjList.add(new ShellObj(Gameutils.shellImg, planeObj.getX() + 3, planeObj.getY() - 16, 14, 29, 5, this));
            Gameutils.gameObjList.add(Gameutils.shellObjList.get(Gameutils.shellObjList.size() - 1));//加入最新的shell到gameObjList操作
        }
        if(count%5==0) {
            Gameutils.enemyObjList.add(new EnemyObj(Gameutils.EnemyImg,(int)(Math.random()*12)*50,0,49,36,5,this));
            Gameutils.gameObjList.add(Gameutils.enemyObjList.get(Gameutils.enemyObjList.size()-1));
            if(Math.random()<0.5)Gameutils.enemyObjList.get(Gameutils.enemyObjList.size()-1).flag=-1;

        }
        if(count%500==0){
            Gameutils.gameObjList.add(bossObj);
        }
        if(count>500&&count%20==0) {
            Gameutils.enemyshellObjList.add(new EnemyShellObj(Gameutils.EnemyShellImg, bossObj.getX() + 40, bossObj.getY() + 40, 14, 29, 5, this));
            Gameutils.gameObjList.add(Gameutils.enemyshellObjList.get(Gameutils.enemyshellObjList.size() - 1));//加入boss子弹
        }

    }

    public static void main(String[] args) {
        GameWin game = new GameWin();
        game.launch();
    }
}
